home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9106 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: tech.cftnet.com!not-for-mail
  2. From: wcowley@cftnet.com (Wes Cowley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ OO question (long)
  5. Date: 28 Feb 1996 10:48:28 GMT
  6. Organization: CFTnet
  7. Message-ID: <4h1bts$45u@tech.cftnet.com>
  8. References: <4h08uq$mve@madeline.INS.CWRU.Edu>
  9. NNTP-Posting-Host: ppp244_8.cftnet.com
  10. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  11.  
  12. Bryan Murphy (bf461@cleveland.Freenet.Edu) wrote:
  13.  
  14. : I can't explain, for instance, this works:
  15. :           Matrix M3 = Matrix1 * Matrix2;
  16. : whereas defining Matrix M3 ahead of time
  17. :           M3 = Matrix1 * Matrix2;
  18. : crashes out on me giving me an error.  I can't really tell why.
  19. : It's a memory allocation assertion error.  I can't figure out
  20. : why I can't do the second method.  I was thinking about this, 
  21. : and I have another question related to this.  
  22.  
  23. : When I define the Matrix M3 and assign it right away, is the 
  24. : Default Blank Constructor called, THEN the Copy Constructor?  
  25.  
  26. Yes. Without a fairly heavy optimization (over multiple compilation units 
  27. in general) it's not possible to tell whether or not there is code in the 
  28. constructor that has to be executed before the assignment operator. Both 
  29. of them have to be executed.  
  30.  
  31. But let's back up a second: you talked about the "default blank constructor" 
  32. and the "copy constructor".  That's not exactly what's going on here.  The
  33. default blank constructor isn't called at all and there are two steps 
  34. missing at the end.
  35.    1. The void constructor you created, not the default one, is called
  36.       to create M3.
  37.    2. operator* creates a new Matrix on the stack and returns it.
  38.    3. The copy constructor is called to copy that Matrix to a temporary
  39.       to hold the return value.  Your copy constructor allocates a
  40.       new array to hold the results and copies them cell by cell.  
  41.       Just fine so far.
  42.    4. Here's the first missing step: you haven't defined an operator= 
  43.       so the default operator= is called to assign the temporary to M3.
  44.       This is where your problem is most likely.  The default operator= 
  45.       does a memberwise assignment from the temporary to M3.  That's fine
  46.       for xdim and ydim, they're just ints.  But MatrixData is a pointer
  47.       that was allocated with new.  Both M3 and the temporary now have
  48.       the same pointer.
  49.    5. Finally, the destructor is called to cleanup the temporary.  It
  50.       deletes MatrixData.  Oops: M3 has a pointer to a deallocated chunk
  51.       of memory.
  52.  
  53.  
  54. : Also, say the copy Constructor was called. When the new data is copied 
  55. : over, is the Destructor called before the copy constructor is used to copy
  56. : the new data over?
  57.  
  58. I think you're confusing the copy constructor with operator=.  But in both
  59. cases the answer is no.  The copy constructor is building a new object:
  60. there's nothing to call the destructor on because nothing has been
  61. constructed yet.  operator= doesn't call the destructor either: you're
  62. not destroying the object, you're assigning a new value to it.  Sometimes
  63. you'll need to clean up part of the object (deallocate some pointers before
  64. you assign new values to them, etc) but you have to do that yourself.
  65.  
  66.  
  67. : Also, my second problem is pretty simple.  It relates to using
  68. : variable arguments.  Is there anything peculiar or different
  69. : about Visual C++'s variable arguments?  
  70.  
  71. I don't see where you're using variable arguments (using ... in a functino
  72. prototype), so I'm assuming you mean the default parameter in your 
  73. constructor.  I doubt VC++ handles those any differently than any other
  74. compiler.
  75.  
  76.  
  77. Wes
  78.